PROJECT_TEST_CLASSES_DIR = 'JRSwizzleTest'

def run_or_die(cmd)
  #puts "*** #{cmd}"
  result = `#{cmd}`
  raise "ERROR: #{cmd} failed" if $?.exitstatus != 0
  result
end

def gen_and_compile(extra_gcc_args)
  Rake::Task[:clean].execute
  run_or_die 'cp -R Test.octest-template Test.octest'

  run_or_die "cd ..; clang #{extra_gcc_args} " \
    "-o #{PROJECT_TEST_CLASSES_DIR}/Test.octest/Contents/MacOS/Test " \
    "-bundle " \
    "-framework Foundation " \
    "-F/Applications/Xcode.app/Contents/Developer/Library/Frameworks " \
    "-framework SenTestingKit " \
    "-I. " \
    "*.m " \
    "#{PROJECT_TEST_CLASSES_DIR}/*.m "
  ENV['OBJC_DISABLE_GC'] = 'YES'
  `xcrun otest Test.octest`
  if $?.exitstatus == 0
    puts "\x1B[1;32m*** TESTS PASSED ***\x1B[0m"
  else
    puts "\x1B[1;31m*** TESTS FAILED ***\x1B[0m"
  end
end

task :mrc do
  puts "\x1B[1m*** MRC ***\x1B[0m"
  gen_and_compile('')
end

task :arc do
  puts "\x1B[1m*** ARC ***\x1B[0m"
  gen_and_compile('-fobjc-arc')
end

task :clean do
  run_or_die 'rm -rf Test.octest'
end

task :default do
  Rake::Task[:mrc].execute
  Rake::Task[:arc].execute
  Rake::Task[:clean].execute
end